home *** CD-ROM | disk | FTP | other *** search
- Path: rose.etri.re.kr!jskim
- From: jskim@rose.etri.re.kr (Kim Jungsun)
- Newsgroups: comp.lang.c++
- Subject: Re: problems w/ relational operator ==
- Date: 17 Jan 1996 08:50:10 GMT
- Organization: ETRI
- Distribution: world
- Message-ID: <4did82$lm4@ns.etri.re.kr>
- References: <30FC83D2.7B6A@hti.net>
- NNTP-Posting-Host: rose1.etri.re.kr
- Keywords: precision
-
- In article <30FC83D2.7B6A@hti.net>, Michael Benrud <mbenrud@hti.net> writes:
- |> I am having some problems using the == operator. I am using BC 4.02.
- |> I have the following snipet of code:
- |>
- |> double i;
- |>
- |> if(i == 1.0)
- |> Xinc = Xinc * 10;
- |>
- |> The problem is that when i equals 1.0, the if statement never executes
- |> Xinc = Xinc * 10. Any ideas? Thanks in advance.
-
-
- Generally, it is a bad idea to do an equality check between non-int numbers,
- because their might be a round off error. To compare two double numbers to
- check an equality, I normally do it this way:
-
- #define BOUND 0.0000001 // It is your responsibility to set
- // this value appropriately
-
- inline bool IS_ZERO(double val)
- {
- return ((val - 0.0) < BOUND) && ((val - 0.0) > -BOUND) ? 1 : 0;
- }
-
- ...
-
- if (IS_ZERO(i - 1.0)) {
- Xinc = Xinc * 10;
- }
-
-
- +=====================================================================+
- | Jungsun Kim, PhD Parallel Programming Section |
- | System Research Department |
- | Email: jskim@rose.etri.re.kr Electronics And Telecommunications |
- | Phone : +82-42-860-6699 Research Institute |
- | Fax : +82-42-860-6645 P.O.Box 106, Yusong |
- | Daejon, 305-600, KOREA |
- +=====================================================================+
-
-
-
-
-
-